package com.nz2004.deaddelayedqueue.mq;


import com.nz2004.deaddelayedqueue.config.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author ReJoice
 */
@Component
public class DeadListener {

    @RabbitListener(queues = RabbitMQConfig.DEAD_QUEUE)
    public void listen(Message message, Channel channel) throws IOException {

        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        List<Map<String, ?>> list = message.getMessageProperties().getXDeathHeader();
        for (Map<String, ?> stringMap : list) {
            System.out.println(stringMap);
        }
        System.out.println("【死信队列】 接收到消息 msg  = " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
